package de.tapps.brainstorming;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.telephony.TelephonyManager;

/**
 * Reports an Answer of a user
 * @author Timo
 * Encrypted - ok
 */
public class ReportAnswer extends AsyncTask<String, Integer, String> {
	int topicID;
	int answerID;
private MainActivity activity = new MainActivity();
private Crypter crypter;
	public ReportAnswer(MainActivity act,int pTopicID, int pAnswerID) {
		topicID = pTopicID;
		answerID = pAnswerID;
		activity = act;
		crypter = new Crypter(activity);

	}

	@Override
	protected String doInBackground(String... arg0) {
		new Thread(new Runnable() {
			public void run() {
				// Create a new HttpClient and Post Header
				HttpClient httpclient = new DefaultHttpClient();
				HttpPost httppost = new HttpPost(
						"http://t-apps-online.de/BrainStorming/addReport.php");
				try {
					TelephonyManager telephonyManager = (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE);
					String imei = telephonyManager.getDeviceId();
				
					
				     // Decode the bytes into a String
					List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
							2);
					nameValuePairs.add(new BasicNameValuePair("topicid",
							""+topicID));
					nameValuePairs.add(new BasicNameValuePair("answerid",
							""+answerID));
					nameValuePairs
							.add(new BasicNameValuePair("reporting",new String(crypter.encrypt(imei))));
					httppost.setEntity(new UrlEncodedFormEntity(
							nameValuePairs));
					// Execute HTTP Post Request
					HttpResponse response = httpclient.execute(httppost);
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
				} catch (IOException e) {
					// TODO Auto-generated catch block
				}
			}
		}).start();
		return null;
	}
}

